﻿using System.Collections.Generic;
using System.IO;
using System.Text;
class IniFile
{
    //用来存数据的容器
    private Dictionary<string, Dictionary<string, string>> dic = new Dictionary<string, Dictionary<string, string>>();

    //加载 *.ini 文件,返回一个IniFile对象
    public static IniFile Load(string path) 
    {
        string mainKey="";
        IniFile iniFile = new IniFile();//创建iniFile对象
        var dic = iniFile.dic;//拿到字典
        using (StreamReader sr=File.OpenText(path))
        {
            while (!sr.EndOfStream)//如果没有到流的结尾就一直读
            {
                string line = sr.ReadLine();                                             //读取一行
                //if (String.IsNullOrWhiteSpace(line)) continue;              //跳过空行

                if (line.Contains('['))                                                        //判断是否是主键
                {
                    int start = line.IndexOf('[');
                    int end = line.IndexOf(']');
                    mainKey =  line.Substring(start+1,end-start-1);        //截取中间的内容
                    dic.Add(mainKey,new Dictionary<string, string> ());//以mainKey做键添加内层字典
                }

                else if (line.Contains('='))                                                      //判断是否是内层键值对
                {
                   string[] kv= line.Split('=');                                           //用=分割
                   dic[mainKey].Add(kv[0].Trim(),kv[1].Trim());             //添加内层字典的键值对
                }
            }
        }
        return iniFile;
    }

    //获取字符串值
    public string GetString(string mainKey, string subKey) 
    {
        return dic[mainKey][subKey];
    }

    //获取int值
    public int GetInt(string mainKey, string subKey)
    {
        return int.Parse(GetString(mainKey, subKey));
    }

    //获取int值
    public float GetFload(string mainKey, string subKey)
    {
        return float.Parse(GetString(mainKey, subKey));
    }

    //获取Double值
    public double GetDouble(string mainKey, string subKey)
    {
        return double.Parse(GetString(mainKey, subKey));
    }

    //获取Bool值
    public bool GetBool(string mainKey, string subKey)
    {
        return bool.Parse(GetString(mainKey, subKey));
    }


    //设置
    public void SetString(string mainKey, string subKey,string value)
    {
         dic[mainKey][subKey]=value;
    }

    //设置
    public void SetInt(string mainKey, string subKey, int value)
    {
        dic[mainKey][subKey] = value.ToString();
    }

    //设置
    public void SetFloat(string mainKey, string subKey, float value)
    {
        dic[mainKey][subKey] = value.ToString();
    }

    //设置
    public void SetDouble(string mainKey, string subKey, double value)
    {
        dic[mainKey][subKey] = value.ToString();
    }

    //设置
    public void SetBool(string mainKey, string subKey, bool value)
    {
        dic[mainKey][subKey] = value.ToString();
    }

    //重写ToString    保存文件直接保存这个String
    public override string ToString()
    {
        StringBuilder sb=new StringBuilder ();
        foreach (var item in dic)
        {
            sb.Append('[');
            sb.Append(item.Key);
            sb.Append("]\n");

            foreach (var subItem in item.Value)//subItem内层字典键值对
            {
                sb.Append(subItem.Key);
                sb.Append('=');
                sb.Append(subItem.Value);
                sb.Append('\n');
            }
        }
        return sb.ToString();
    }

    //保存到文件
    public void SaveToFile(string path) 
    {
        File.WriteAllText(path,ToString());
    }

    //为了方便直接直接读写字符串增加一个索引器
    public string this[string mainKey,string subKey]
    {
        get => dic[mainKey][subKey];
        set => dic[mainKey][subKey] = value;
    }

}
